//发送模板消息
exports.templateMessagesSent = function(req, res) {

	//获取restful参数
	var bdoyParams = req.routeInfo.bodyParams;
	console.log('%c这是接收到的参数', 'color:#fac011')
	console.log(bdoyParams)

	//最终反回结果
	var resultData = {};

	//保存access_token
	var accessToken = "";

	//保存jsapi_ticket
	var jsapiTicket = "";

	//任务流创建
	async.waterfall([

		//获取accessToken
		(cb)=> {

			R.SEND_HTTP(
				req, res, {
					url: "http://" + config.publicConfig.SERVER_IP + ":8800/weixin/getAccessToken",
					method: 'GET'
				},
				function(err, data) {

					if(!err.success) {
						return RES.errorResponse(res, "公众号config配置签名获取失败");
					}

//					console.log('getAccessToken--------')
//					console.log(err.result)

					access_token = err.result;

					cb();

				}
			);

		},

		//获取模板内容
		(cb)=> {
			if(bdoyParams.type == "xinDingDan") {
				//新订单通知
				var postDatas = {
					"touser": bdoyParams.openid || "o2UdI0uia55ETG5ye30HbcYISyVc",
					"template_id": "Ap2gcsrWGkbFd0OIsC3r0-RbKheSlc541JBQAv2xtYE",
					"url": "http://crm.hxzbi.com/CRM/#/orderList", //跳转
//					"url": bdoyParams.goUrls, //跳转
					"data": {
						"first": {
							"value": "你有一个来自“"+bdoyParams.name+"”销售的订单。", //标题
							"color": "#173177"
						},
						"keyword1": {
							"value": bdoyParams.productName, //产品名
							"color": "#173177"
						},
						"keyword2": {
							"value": common.getTimes('','shifen','-'), //时间
							"color": "#173177"
						},
						"remark": {
							"value": "点击查看详情!", //结束语
							"color": "#173177"
						}
					}
				}

			}else if(bdoyParams.type == "xinKeHu") {
				//新增客提醒
				var postDatas = {
					"touser": bdoyParams.openid || "o2UdI0uia55ETG5ye30HbcYISyVc",
					"template_id": "GJPDv5NOELqRfBZa88r5Hk05vI4omYc1ex2eIZkQ3GI",
					"url": "http://crm.hxzbi.com/CRM/#/userDetail?customerId=" + bdoyParams.customerId, //跳转
//					"url": bdoyParams.goUrls, //跳转
					"data": {
						"first": {
							"value": "您好，您的员工提交了一个新客户记录", //标题
							"color": "#173177"
						},
						"keyword1": {
							"value": bdoyParams.name, //客户名
							"color": "#173177"
						},
						"keyword2": {
							"value": bdoyParams.sellName, //销售名
							"color": "#173177"
						},
						"keyword3": {
							"value": common.getTimes('','cn','-'), //时间
							"color": "#173177"
						},
						"remark": {
							"value": "点击查看详情!", //结束语
							"color": "#173177"
						}
					}
				}

			}else if(bdoyParams.type == "fanKuiTiXing") {
				//反馈提醒
				var postDatas = {
					"touser": bdoyParams.openid || "o2UdI0uia55ETG5ye30HbcYISyVc",
					"template_id": "i4DE73-Xghm0rcQFEpSnPmmtIjc-gzdlu188AJ_ryxc",
					"url": "http://crm.hxzbi.com/CRM/#/replyObjection?sq_id=" + bdoyParams.sq_id, //跳转
//					"url": bdoyParams.goUrls, //跳转
					"data": {
						"first": {
							"value": "收到一条员工反馈", //标题
							"color": "#173177"
						},
						"keyword1": {
							"value": bdoyParams.name, //反馈人
							"color": "#173177"
						},
						"keyword2": {
							"value": common.getTimes('','','-'), //时间
							"color": "#173177"
						},
						"keyword3": {
							"value": "好校长", //销售名
							"color": "#173177"
						},
						"remark": {
							"value": "请及时进行处理", //结束语
							"color": "#173177"
						}
					}
				}

			}else if(bdoyParams.type == "fanKuiHuiFu") {
				//反馈回复提醒
				var postDatas = {
					"touser": bdoyParams.openid || "o2UdI0uia55ETG5ye30HbcYISyVc",
					"template_id": "FbXz7GXks9oxn_2Jos9sRbKA1qB6JRanj53Jn-knjZ4",
					"url": "http://crm.hxzbi.com/CRM/#/lookObjection?sq_id=" + bdoyParams.sq_id, //跳转
//					"url": bdoyParams.goUrls, //跳转
					"data": {
						"first": {
							"value": "你有一条反馈的信息", //标题
							"color": "#173177"
						},
						"keyword1": {
							"value": common.getTimes('','cn','-'), //时间
							"color": "#173177"
						},
						"keyword2": {
							"value": bdoyParams.content, //反馈内容
							"color": "#173177"
						},
						"keyword3": {
							"value": bdoyParams.reply, //反馈回复
							"color": "#173177"
						},
						"remark": {
							"value": "点击查看全部反馈", //结束语
							"color": "#173177"
						}
					}
				}

			}else if(bdoyParams.type == "genJinBeiDianPing") {
				//跟进被点评
				var postDatas = {
					"touser": bdoyParams.openid || "o2UdI0uia55ETG5ye30HbcYISyVc",
					"template_id": "7Mcj1yLMNaBBoxYQnO7hDD0e_9GkCylW4xPGHRrkZ1A",
					"url": bdoyParams.goUrls, //跳转
					"data": {
						"first": {
							"value": "您有一条客户跟进被点评！", //标题
							"color": "#173177"
						},
						"keyword1": {
							"value": bdoyParams.name, //点评人名称
							"color": "#173177"
						},
						"keyword2": {
							"value": common.getTimes('','cn','-'), //时间
							"color": "#173177"
						},
						"keyword3": {
							"value": bdoyParams.organizationName, //机构名称
							"color": "#173177"
						},
						"remark": {
							"value": "点击查看详情", //结束语
							"color": "#173177"
						}
					}
				}

			}

			var paramsData = {
				"access_tokenData": access_token,
				"postData": JSON.stringify(postDatas)
			};

//			console.log("我是传参--------------------")
//			console.log(paramsData)

			R.SEND_HTTP(
				req, res, {
					url: "http://" + config.publicConfig.SERVER_IP + "/weixinDemo_CRM/getWeChatMessage.php",
					method: 'POST',
					data: paramsData
				},
				function(err, data) {

					console.log("我是返回--------------------")
					console.log(err)

					// if(err.result.errmsg != 'ok') {
						// console.log(err.result.errmsg)
						// return RES.errorResponse(res, "模板消息发送失败");
					// }

					console.log("模板消息发送成功");

					cb();

				}
			);

		},

	], ()=> {

		//返回最终拼接结果
		RES.successResponse(res, resultData);

	});

}